﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace Server
{
    class Message
    {
        public void sendMessage(Socket clientSocket, string protocolMessage)
        {
            try
            {
                byte[] sendmess = Encoding.UTF8.GetBytes(protocolMessage);

                System.Diagnostics.Debug.WriteLine("Message sent: " + protocolMessage);

                clientSocket.Send(sendmess);
                sendmess = null;
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err);
            }
        }

        public string recMess(Socket clientSocket)
        {
            byte[] buffer = new byte[1024];
            string message = null;
            try
            {
                int recivedbytes = clientSocket.Receive(buffer);
                if (recivedbytes == 0)
                {
                    message = null;
                }
                else
                {
                    message = Encoding.UTF8.GetString(buffer);
                    System.Diagnostics.Debug.WriteLine("Command Message Recived: " + message);
                }
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err);
                message = null;
            }

            return message;
        }



        //public string recMess(Socket incomming)
        //{
        //    byte[] buffer = new byte[1024];

        //    try
        //    {
        //        incomming.Receive(buffer);
        //        System.Diagnostics.Debug.WriteLine("Message Recived: " + Encoding.UTF8.GetString(buffer));
        //    }
        //    catch (Exception err)
        //    {
        //        System.Diagnostics.Debug.WriteLine(err);
        //    }

        //    return Encoding.UTF8.GetString(buffer);
        //}
    }
}
